package org.sercho.masp.notification.test;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.jms.IllegalStateException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.sercho.masp.notification.NotificationSystem;
import org.sercho.masp.notification.MessageCenter.Message.MessageOption;
import org.sercho.masp.notification.MessageCenter.MessageCenter;
import org.sercho.masp.notification.MessageCenter.Priority;
import org.sercho.masp.notification.MessageCenter.Urgency;

/**
 * Servlet implementation class NotificationTestServlet
 */
public class NotificationTestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static final String MESSAGE_SOURCE = "testQuelle";
	private static final String SEND_MESSAGE = "sendMessage";
	private static final String REGISTER_MESSAGE = "registerMessage";
	private NotificationSystem notifSys;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NotificationTestServlet() {
		super();
		this.notifSys = NotificationSystem.getInstance();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");

		if (type.equalsIgnoreCase(SEND_MESSAGE)) {
			sendMessage(request);
		} else if (type.equalsIgnoreCase(REGISTER_MESSAGE)) {
			registerMessage(request);
		}

		response.sendRedirect("/MASP/notification.html");
	}

	private void registerMessage(HttpServletRequest request) {
		String msgTitle = request.getParameter("MessageTitle");
		String msgContent = request.getParameter("MessageContent");
		String[] msgTimes = request.getParameter("MessageTime").split(";");
		String[] msgUrgencies = request.getParameter("MessageUrgency").split(
				";");
		Map<String, String> timesOfNotification = new TreeMap<String, String>();
		Map<String, Integer> urgencies = new TreeMap<String, Integer>();
		for (int i = 0; i < msgTimes.length; i++) {
			String timeId = msgTimes[i].replaceAll("/\\s/g", "");
			timesOfNotification.put(timeId, msgTimes[i]);
			int urgency = 3;
			if (msgUrgencies[i] != null && !msgUrgencies[i].isEmpty()) {
				try {
					urgency = Integer.parseInt(msgUrgencies[i]);
				} catch (NumberFormatException e) {
				}
			}
			urgencies.put(timeId, urgency);
		}
		try {
			this.notifSys.registerMessage(MESSAGE_SOURCE, msgTitle, msgContent,
					timesOfNotification, urgencies);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void sendMessage(HttpServletRequest request) {
		String user = request.getParameter("User");
		String msgTitle = request.getParameter("MessageTitle");
		String msgContent = request.getParameter("MessageContent");
		String msgTime = request.getParameter("MessageTime").replaceAll(
				"/\\s/g", "");
		int msgUrgency = 3;
		try {
			msgUrgency = Integer.parseInt(request
					.getParameter("MessageUrgency"));
		} catch (NumberFormatException e) {
		}

		Urgency urgency = msgUrgency <= 1 ? Urgency.VERY_LOW
				: msgUrgency == 2 ? Urgency.LOW
						: msgUrgency == 3 ? Urgency.MIDDLE
								: msgUrgency == 4 ? Urgency.HIGH
										: Urgency.VERY_HIGH;
		MessageCenter.send(MESSAGE_SOURCE, msgTitle, msgContent, user,
				Priority.High, urgency, msgTime,
				getMessageOptions(user, msgTitle, msgTime));

	}

	private MessageOption[] getMessageOptions(final String user,
			final String msgTitle, final String msgTime) {
		MessageOption[] options = new MessageOption[5];

		Runnable optionCallback1 = new Runnable() {
			@Override
			public void run() {
				NotificationSystem.getInstance().saveUserUrgency(user,
						NotificationTestServlet.MESSAGE_SOURCE, msgTitle,
						msgTime, 1);
			}
		};
		options[0] = new MessageOption("nicht dringend", optionCallback1);

		Runnable optionCallback2 = new Runnable() {
			@Override
			public void run() {
				NotificationSystem.getInstance().saveUserUrgency(user,
						NotificationTestServlet.MESSAGE_SOURCE, msgTitle,
						msgTime, 2);
			}
		};
		options[1] = new MessageOption("wenig dringend", optionCallback2);

		Runnable optionCallback3 = new Runnable() {
			@Override
			public void run() {
				NotificationSystem.getInstance().saveUserUrgency(user,
						NotificationTestServlet.MESSAGE_SOURCE, msgTitle,
						msgTime, 3);
			}
		};
		options[2] = new MessageOption("mittel-dringend", optionCallback3);

		Runnable optionCallback4 = new Runnable() {
			@Override
			public void run() {
				NotificationSystem.getInstance().saveUserUrgency(user,
						NotificationTestServlet.MESSAGE_SOURCE, msgTitle,
						msgTime, 4);
			}
		};
		options[3] = new MessageOption("sehr dringend", optionCallback4);

		Runnable optionCallback5 = new Runnable() {
			@Override
			public void run() {
				NotificationSystem.getInstance().saveUserUrgency(user,
						NotificationTestServlet.MESSAGE_SOURCE, msgTitle,
						msgTime, 5);
			}
		};
		options[4] = new MessageOption("unheimlich dringend", optionCallback5);

		return options;
	}

}
